package com.liuya.safe.entitle.controller;

import com.liuya.base.BaseController;
import com.liuya.common.WebUtil;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.liuya.safe.entitle.service.EntitleManagerService;
import com.liuya.safe.entitle.service.PrivilegeManagerService;
import com.liuya.safe.model.SafePrivilege;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 非角色权限管理
 * @author liuya
 * @date 20160826
 */
@Controller
@Api(value = "非角色权限管理", description = "非角色权限管理")
@RequestMapping(value = "priviPolicy")
public class PrivilegePolicyContrller extends BaseController {
    private static final Log log = LogFactory.getLog(PrivilegePolicyContrller.class);

    @Autowired
    @Qualifier("privilegeManagerService")
    PrivilegeManagerService pvlgMng;
    @Autowired
    @Qualifier("entitleManagerService")
    EntitleManagerService entitleMng;

    @ApiOperation(value = "查询会话")
//    @RequiresPermissions("sys.nonRolePriviMng.policy")
    @RequestMapping(value = "/policy")
    public Object policy(HttpServletRequest req, HttpServletResponse rep, ModelMap map){
        String oper = req.getParameter("oper");
        String id = WebUtil.getStringParameter(req, "id", "-23");

        SafePrivilege pvlg = pvlgMng.getPrivilege(id);

        boolean isQueryEntitle = false;
        boolean isDecisionEntitle = false;
        Collection queryEntitlements = entitleMng.getQueryEntitlements(id);
        Collection decisionEntitlements = null;
        if (queryEntitlements != null && queryEntitlements.size() > 0) {
            // this is a query privilege
            isQueryEntitle = true;
        } else {
            decisionEntitlements = entitleMng.getDecisionEntitlements(id);
            if (decisionEntitlements != null && decisionEntitlements.size() > 0) {
                // this is a decison privilege
                isDecisionEntitle = true;
            }
        }

        // no entitlement is set for this privilege
        if (!isQueryEntitle && !isDecisionEntitle) {
            map.addAttribute("privilege", pvlg);
            map.addAttribute("id", id);
            return "privilege/chooseType";
        }

        if ("testPolicy".equals(oper)) {
            if (isDecisionEntitle) {
                return "redirect:/decisionEntitlementTest/decision.do?oper=loadFresh&id=" + id;
            } else if (isQueryEntitle) {
                return "redirect:/queryEntitlementTest/decision.do?oper=loadFresh&id=" + id;
            }
        } else if ("editPolicy".equals(oper)) {
            if (isDecisionEntitle) {
                return "redirect:/decisionEntitlement/decision.do?oper=loadFresh&id=" + id;
            } else if (isQueryEntitle) {
                return "redirect:/queryEntitlement/decision.do?oper=loadFresh&id=" + id;
            }
        }
        return null;
    }

}
